<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\User;
use app\admin\model\Contact as cateModel;
class Contact extends User
{
    public function index()
    {
        $model = new cateModel();
//        $cate = $model->select();
        $post = $this->request->post();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['business'] = ['like', '%' . $post['keywords'] . '%'];
        }
        $cate = empty($where) ? $model->paginate(20) : $model->where($where)->paginate(20);
        $this->assign('contact',$cate);
        return $this->fetch();
    }


    public function publish()
    {
    	//获取菜单id
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	$model = new cateModel();
		//是正常添加操作
		if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			//验证  唯一规则： 表名，字段名，排除主键值，主键名
	            $validate = new \think\Validate([


	            ]);
	            //验证部分数据合法性
	            if (!$validate->check($post)) {
	                $this->error('提交失败：' . $validate->getError());
	            }
	            //验证菜单是否存在
	            $cate = $model->where('id',$id)->find();
	            if(empty($cate)) {
	            	return $this->error('id不正确');
	            }
	            if(false == $model->allowField(true)->save($post,['id'=>$id])) {
	            	return $this->error('修改失败');
	            } else {

	            	return $this->success('修改成功','admin/contact/index');
	            }
    		} else {
    			//非提交操作
    			$cate = $model->where('id',$id)->find();
    			$contact = $model->select();
    			$contact_all = $model->catelist($contact);
    			$this->assign('contact',$contact_all);
    			if(!empty($cate)) {
    				$this->assign('cate',$cate);
    				return $this->fetch();
    			} else {
    				return $this->error('id不正确');
    			}
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			//验证  唯一规则： 表名，字段名，排除主键值，主键名
	            $validate = new \think\Validate([


	            ]);
	            //验证部分数据合法性
	            if (!$validate->check($post)) {
	                $this->error('提交失败：' . $validate->getError());
	            }
	            if(false == $model->allowField(true)->save($post)) {
	            	return $this->error('添加失败');
	            } else {
	            	return $this->success('添加成功','admin/contact/index');
	            }
    		} else {
    			//非提交操作
    			$pid = $this->request->has('pid') ? $this->request->param('pid', null, 'intval') : null;
    			if(!empty($pid)) {
    				$this->assign('pid',$pid);
    			}
    			$cate = $model->select();
    			$contact = $model->catelist($cate);
    			$this->assign('contact',$contact);
    			return $this->fetch();
    		}
    	}

    }


    public function delete(){
        $data = input('id');
        $del = Db::name('contact')->delete($data);
        if ($del){
            $this->success('删除成功！','admin/contact/index');
        }else{
            $this->error('删除失败！');
        }
    }
}
